package com.example.rabbitmq.example.rabbitmq.hello;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class HelloWorldProcedure {

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.3");
        factory.setVirtualHost("/");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);
        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        //1. 声明消息队列: 消息队列名称 可持久化 排他的 自动删除 队列属性
        channel.queueDeclare("queue1", false, false, true, null);

        //2.声明交换机: 交换机名称 类型 可持久化的 自动删除的 属性Map集合
        channel.exchangeDeclare("exchange1", BuiltinExchangeType.DIRECT, false,false, null);

        channel.queueBind("queue1", "exchange1", "hello.world");
        channel.basicPublish("exchange1", "hello.world", null,"hello world".getBytes(StandardCharsets.UTF_8));
        channel.close();
        connection.close();
    }
}
